<%@ page import="com.model.User" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: 张爽
  Date: 2024/9/7
  Time: 11:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib  uri ="http://java.sun.com/jsp/jstl/core" prefix="c"%>
   <%@ page import="java.sql.*" %>
   <%
      Class.forName("com.mysql.jdbc.Driver");
      String url = "jdbc:mysql://localhost:3306/mydb";
      String user = "root";
      String password = "songhao0517";

      Connection conn = null;
      Statement stmt = null;
      ResultSet rs = null;

      try {
         conn = DriverManager.getConnection(url, user, password);
         stmt = conn.createStatement();
          // 处理删除请求
          String idToDelete = request.getParameter("id");
          if (idToDelete != null) {
              String deleteSql = "DELETE FROM users WHERE id = ?";
              PreparedStatement pstmt = conn.prepareStatement(deleteSql);
              pstmt.setString(1, idToDelete);
              int rowsAffected = pstmt.executeUpdate();
              if (rowsAffected > 0) {
                  out.println("删除成功！");
              } else {
                  out.println("删除失败，请检查ID是否正确。");
              }
              pstmt.close();
          }

          // 查询数据
         String sql = "SELECT * FROM users";
         rs = stmt.executeQuery(sql);
   %>

<html>
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

</head>
<body style="padding-top: 20px;">

<div class="container">
    <div class="col-md-8 col-offset-2" >
        <h1>用户列表</h1><br>
        <table class="table">
            <thead>
            <tr>
                <td>用户名</td>
                <td>用户邮箱</td>
                <td>密码</td>
                <td>操作</td>
            </tr>
            </thead>

            <tbody>
                <% while (rs.next()) { %>
            <tr>

                <td><%= rs.getString("username") %></td>
                <td><%= rs.getString("email") %></td>
                <td><%= rs.getString("password") %></td>

                <td>
                    <form action="<%= request.getRequestURI() %>" method="post">
                        <input  type="hidden" name="id" value="<%= rs.getString("id") %>">
                        <input class="btn btn-danger btn-sm" type="submit" value="删除用户" onclick="return confirm('确定删除吗？')">
                    </form>
                </td>

            </tr>
                <% } %>

        </tbody>
        </table>
        <a href="tjyh.jsp"><input value="添加用户" style="width: 70px; height: 24px"></input></a>
        <a href="home.jsp"><input value="回到首页" style="width: 70px; height: 24px"></input></a>


    </div>
</div>
</body>
</html>
<%
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (rs != null) { try { rs.close(); } catch (SQLException e) {} }
    if (stmt != null) { try { stmt.close(); } catch (SQLException e) {} }
    if (conn != null) { try { conn.close(); } catch (SQLException e) {} }
    }
%>